home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15106 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.4 KB

  1. Path: ix.netcom.com!netnews
  2. From: jlilley@ix.netcom.com (John Lilley)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Multiple Inheritance Pointer Problem
  5. Date: 3 Apr 1996 15:04:12 GMT
  6. Organization: Netcom
  7. Message-ID: <4ju41c$96q@cloner2.ix.netcom.com>
  8. References: <31622A26.3633@nmaa.org>
  9. NNTP-Posting-Host: den-co11-06.ix.netcom.com
  10. Mime-Version: 1.0
  11. Content-Type: Text/Plain; charset=US-ASCII
  12. X-NETCOM-Date: Wed Apr 03  7:04:12 AM PST 1996
  13. X-Newsreader: WinVN 0.99.7
  14.  
  15. In article <31622A26.3633@nmaa.org>, jphelps@nmaa.org says...
  16. >
  17. >I recently used multiple inheritence to help me create a few
  18. >socket classes. The details of these socket classes are not
  19. >important; however, in my pursual of bugs I found that some
  20. >of the pointers to these classes caused core dumps while
  21. >other classes seem to work fine. So, as part of my debug
  22. >process, I printed out pointers to theses classes before I
  23. >passed them around, and then printed these pointers after I
  24. >passed them in to various functions, and to my surprise some
  25. >of these pointers were off by four bytes! (This took place on
  26. >an HP 735, running HP-UX 9.03 -- I think these environment
  27. >specs are correct; its late and I'm groggy.)
  28. >
  29. >
  30. >#include <iostream.h>
  31. >
  32. >class foo {
  33. >public:
  34. >    void PrintMe(void) { cerr << "this = " << this << endl; }
  35. >};
  36. >
  37. >class bar
  38. >{
  39. >#ifdef OFF_BY_4
  40. >public:
  41. >    int b;
  42. >#endif
  43. >};
  44. >
  45. >class foobar : public foo, public bar
  46. >{
  47. >#ifdef OFF_BY_4
  48. >public:
  49. >    int fb;
  50. >#endif
  51. >};
  52. >
  53. >class barfoo : public bar, public foo
  54. >{
  55. >#ifdef OFF_BY_4
  56. >public:
  57. >    int bf;
  58. >#endif
  59. >};
  60. >
  61. >main()
  62. >{
  63. >    foobar fb;
  64. >    barfoo bf;
  65. >
  66. >    cerr << "&fb = " << &fb << endl;
  67. >    fb.PrintMe();
  68. >    cerr << "&bf = " << &bf << endl;
  69. >    bf.PrintMe();
  70. >
  71. >    return(0);
  72. >}
  73. >
  74. >&fb = 0x0012FFAC
  75. >this = 0x0012FFAC
  76. >&bf = 0x0012FFA8
  77. >this = 0x0012FFA9
  78. >...
  79.  
  80.  
  81. In multiple inheritance, the address of a base class may not be
  82. the same as the address of a derived class.  This is true because
  83. more than one base cannot be placed at the same physical address
  84. when composed to form the derived object.
  85.  
  86. What you are printing out is:
  87.  
  88. Address of foobar
  89.    cerr << "&fb = " << &fb << endl;
  90. Address of "foo" part of foobar
  91.    fb.PrintMe();
  92. Address of barfoo
  93.    cerr << "&bf = " << &bf << endl;
  94. Address of "foo" part of barfoo
  95.    bf.PrintMe();
  96.  
  97. Your output is sensible since (1) The compiler may arrange the order of 
  98. multiple bases any way it likes, (2) foobar and bar probably have the foo and 
  99. bar portions arranged differently.
  100.  
  101. john lilley
  102.  
  103.